package com.example.pets.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.pets.R;
import com.example.pets.model.entities.database.ShareprefenseUtil;
import com.example.pets.model.entities.User;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;


public class ProfileActivity extends AppCompatActivity {

    private static final int REQUEST_IMAGE = 1001;
    private User user;
    ImageView profileImage;
    Button changePhotoButton;
    Button submitChangesButton;
    EditText nickname_edit_text,email_edit_text;

    String imagePath="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

         profileImage = findViewById(R.id.profile_image);
         changePhotoButton = findViewById(R.id.change_photo_button);
         submitChangesButton = findViewById(R.id.submit_changes_button);
        nickname_edit_text = findViewById(R.id.nickname_edit_text);
        email_edit_text = findViewById(R.id.email_edit_text);

        changePhotoButton.setOnClickListener(v -> selectImage());
        submitChangesButton.setOnClickListener(v -> saveChanges());
         setdata();
    }
    private void setdata() {
        user= ShareprefenseUtil.getinstanse().getLoginUser();
        Glide.with(this)
                .load(user.getImageUrl())
                .into(profileImage);
        nickname_edit_text.setText(user.getUname());
        email_edit_text.setText(user.getEmail());
        email_edit_text.setEnabled(false);
    }
    private void selectImage() {
        PictureSelector.create(ProfileActivity.this)
                .openGallery(SelectMimeType.ofImage())
                .setMaxSelectNum(1)
                .setImageEngine(new YImageEngine())
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 获取选择的图片结果
                    List<LocalMedia> selectList = PictureSelector.obtainSelectorList(data);
                    if (!selectList.isEmpty()) {
                         imagePath = selectList.get(0).getPath();
                        updateProfileImage(imagePath);
                    }
                    break;
            }
        }
    }

    private void updateProfileImage(String imagePath) {
        ImageView profileImage = findViewById(R.id.profile_image);
        Glide.with(this)
                .load(imagePath)
                .into(profileImage);
    }

    private void saveChanges() {
        EditText nicknameEditText = findViewById(R.id.nickname_edit_text);
        EditText emailEditText = findViewById(R.id.email_edit_text);

        String nickname = nicknameEditText.getText().toString();
        String email = emailEditText.getText().toString();


        if(!imagePath.isEmpty()){
            user.setImageUrl(imagePath);
        }

        if(!nickname.isEmpty()){
            user.setUname(nickname);
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                ShareprefenseUtil.getinstanse().insertLoginUser(user);


            }
        }).start();

        Toast.makeText(ProfileActivity.this,"修改成功",Toast.LENGTH_LONG);
        Log.d("====",user.toString());
        finish();
        // 在这里执行保存逻辑，例如发送到服务器或更新本地数据库
        // 此处仅为示例，实际应用中需要根据需求进行调整
    }
}